﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace CloudStorageLight.Core
{

    /// <summary>
    /// 例外時の種類
    /// </summary>
    public enum BlobExceptionCode
    {
        ApplicationError,
        AlreadyExists,
        NotFoundUser,
        NotAuthorization,
        Prohibited,
        Locked,
        NotFoundFile,
        NotCheckOut,
        InvalidOperationRemoveNotEmptyFolder,
        InvalidQuery,
        ExceedFileUrlLength,
        ExceedFolderUrlLength,
        ExistsUser = 117,
        InvalidFileFolderChar,
        AlreadyCheckouted,
        InvalidDirectoryPath,
        DuplicateRoleToken,
        NonDeletableFolder,
        NotPasscodeAccess,
        NotAvailable,
        StorageQuotaExceeded,
        CloudAccountLocked,
        InvalidIPAddress,
        CannotRenameSameFileName,
        RestrictFileType,
        NoDecryptableFile,
        ExpiredTicket,
        ExceedUploadFileSize,
        ExceedOperatableFileSize,
        CanNotProtect,
        InvalidCustomCredentials,
        AccessDenied,
        CheckoutLocked,
        TooLongFileName,
        NotAcceptablePath,
    }


    /// <summary>
    /// 共通アプリケーション例外
    /// </summary>
    [Serializable]
    [ExcludeFromCodeCoverage]
    public class BlobException:Exception
    {
        public BlobExceptionCode Code
        {
            get { return (BlobExceptionCode)Data["Code"]; }
            private set 
            { 
                Data["Code"] = value;
            }
        }

        public int HttpStatus
        {
            get { return (int)Data["HttpStatus"]; }
            private set
            {
                Data["HttpStatus"] = value;
            }
        }

        public BlobException()
        {

        }
        public BlobException(BlobExceptionCode code, params object[] args)
            : base(GetExceptionMessage(code, args))
        {
            this.Code = code;
            if (code == BlobExceptionCode.NotPasscodeAccess)
            {
                this.HttpStatus = 403;
            }
            else
            {
                this.HttpStatus = 500;
            }
        }

        public BlobException(string message)
            : base(message)
        {
            this.Code = BlobExceptionCode.ApplicationError;
            this.HttpStatus = 500;
        }

        private static string GetExceptionMessage(BlobExceptionCode blobExCode, params object[] args)
        {
            if (blobExCode == BlobExceptionCode.AlreadyExists) return Messages.MsgAlreadyExists;
            if (blobExCode == BlobExceptionCode.Locked) return Messages.MsgLocked;
            if (blobExCode == BlobExceptionCode.AlreadyCheckouted) return Messages.MsgAlreadyCheckouted;
            if (blobExCode == BlobExceptionCode.InvalidDirectoryPath) return Messages.MsgInvalidDirectoryPath;
            if (blobExCode == BlobExceptionCode.NotCheckOut) return Messages.MsgCanNotCheckInNotCheckOut;
            if (blobExCode == BlobExceptionCode.NotFoundUser) return Messages.MsgNotFoundUser;
            if (blobExCode == BlobExceptionCode.NotFoundFile) return Messages.MsgNotFoundFile;
            if (blobExCode == BlobExceptionCode.NotAuthorization) return Messages.MsgNotAuthorization;
           if (blobExCode == BlobExceptionCode.InvalidOperationRemoveNotEmptyFolder) return Messages.MsgInvalidOperationRemoveNotEmptyFolder;
            if (blobExCode == BlobExceptionCode.InvalidQuery) return Messages.MsgInvalidQuery;
            if (blobExCode == BlobExceptionCode.ExceedFileUrlLength) return string.Format(Messages.MsgFileUrlLength, BlobUtil.MaxFileLength);
            if (blobExCode == BlobExceptionCode.ExceedFolderUrlLength) return string.Format(Messages.MsgFolderUrlLength, BlobUtil.MaxFolderLength);
            if (blobExCode == BlobExceptionCode.ExistsUser) return Messages.MsgExistsUser;
            if (blobExCode == BlobExceptionCode.InvalidFileFolderChar) return Messages.MsgInvalidFileFolderChar;
            if (blobExCode == BlobExceptionCode.DuplicateRoleToken) return Messages.MsgDuplicateRole;
            if (blobExCode == BlobExceptionCode.NonDeletableFolder) return Messages.MsgNonDeletableFolder;
            if (blobExCode == BlobExceptionCode.NotPasscodeAccess) return Messages.MsgNotPasscodeAccess;
            if (blobExCode == BlobExceptionCode.NotAvailable) return Messages.MsgNotAvailable;
            if (blobExCode == BlobExceptionCode.StorageQuotaExceeded) return Messages.MsgStorageQuotaExceeded;
            if (blobExCode == BlobExceptionCode.InvalidIPAddress) return Messages.MsgInvalidIPAddress;
            if (blobExCode == BlobExceptionCode.CannotRenameSameFileName) return Messages.MsgCannotRenameSameFileName;
            if (blobExCode == BlobExceptionCode.RestrictFileType) return string.Format(Messages.MsgRestrictFileType, args);
            if (blobExCode == BlobExceptionCode.NoDecryptableFile) return Messages.MsgNoDecryptableFile;
            if (blobExCode == BlobExceptionCode.ExpiredTicket) return Messages.MsgExpiredTicket;
            if (blobExCode == BlobExceptionCode.Prohibited) return Messages.MsgProhibited;
            if (blobExCode == BlobExceptionCode.ExceedUploadFileSize) return Messages.MsgExceedUploadFileSize;
            if (blobExCode == BlobExceptionCode.ExceedOperatableFileSize) return Messages.MsgExceedOperatableFileSize;
            if (blobExCode == BlobExceptionCode.CanNotProtect) return string.Format(Messages.MsgCanNotProtect, args);
            if (blobExCode == BlobExceptionCode.AccessDenied) return Messages.MsgAccessDenied;
            if (blobExCode == BlobExceptionCode.InvalidCustomCredentials) return Messages.MsgInvalidCustomCredentials;
            if (blobExCode == BlobExceptionCode.CheckoutLocked) return string.Format(Messages.MsgCheckoutLocked, args);
            if (blobExCode == BlobExceptionCode.NotAcceptablePath) return Messages.MsgNotAcceptablePath;
            if (blobExCode == BlobExceptionCode.TooLongFileName) return Messages.MsgTooLongFileName;
            return Messages.MsgGeneral + "(" + blobExCode.ToString() + ")";
        }
        
    }
}
